using System;
using System.Collections.Generic;
using System.ComponentModel;

using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CodePlex.SpaceBlock;
using CodePlex.SpaceBlock.FS;
using CodePlex.Resourceful;


namespace CodePlex.SpaceBlock.UI.Controls
{
    public partial class OptionsForm : CodePlex.SpaceBlock.UI.Controls.BaseForm
    {
        private static class Actions
        {
            public static readonly string AddFileSystem = "AddFileSystem";
            public static readonly string RemoveFileSystem = "RemoveFileSystem";

        }

        private MainForm _form;
        private FileSystemManager _manager;
        private readonly ActionManager _actions;
        private readonly List<FileSystemInfo> _currentInfos;

        public OptionsForm()
        {
            InitializeComponent();

           
            _manageGroup.Text = Properties.Messages.Options_ManageDrives_Group;

            _currentInfos = new List<FileSystemInfo>();

            this.SetupDriveList();

            _actions = new ActionManager(this, UIManager.Instance.SmallImageList, _driveList.ContextMenuStrip, null);

            this.SetupActions();

            this.SetupDialogWork(_cancelButton, _saveButton,this.OnSave);

        }

        public void Initialize(MainForm form, FileSystemManager manager, bool zombieMode)
        {
            _form = form;
            _manager = manager;

            _currentInfos.Clear();
            _currentInfos.AddRange(manager.FileSystemInfos);

            this.UpdateData(0);
        }











        private bool OnSave()
        {
            for (int i = 1; i < _currentInfos.Count; i++)
            {
                FileSystemInfo info = _currentInfos[i];

                try
                {
                    IFileSystem fs = _manager.ToFileSystem(info);
                    _currentInfos[i].Caption = fs.Caption;
                }
                catch (Exception ex)
                {
                    throw CodePlexException.Format(ex, Properties.Messages.Exceptions_ValidateFileSystem, info.Caption, ex.Message);
                }
            }
           return AuthUtils.Validate(_form, _currentInfos);
              
        }

        private void SetupDriveList()
        {
            _driveList.ContextMenuStrip = new ContextMenuStrip();
            _driveList.MultiSelect = false;
            _driveList.HideSelection = false;

            _driveList.SelectedIndexChanged += delegate
            {
                if (_driveList.SelectedIndices.Count != 1)
                {
                    return;
                }

               FileSystemInfo selectedInfo = _driveList.SelectedItems[0].Tag as FileSystemInfo;

               int selectedIndex = _driveList.SelectedIndices[0];

                _settingsPanel.Controls.Clear();

                Control control = null;
                if (selectedInfo.Type == "S3")
                {

                    S3SettingsControl settingsControl = new S3SettingsControl();
                    settingsControl.Initialize(selectedInfo);

                    _settingsBox.Text = Properties.Messages.Options_S3_Title;
                    control = settingsControl;
                }
                if (selectedInfo.Type == "Local")
                {
                    _settingsPanel.Controls.Clear();

                    Label localLabel = new Label();
                    localLabel.Text = Properties.Messages.Options_Local_Caption;

                    _settingsBox.Text = Properties.Messages.Options_Local_Title;
                    control = localLabel;

                }

                UIUtils.FillWith(_settingsPanel, control);

                control.Focus();

                bool isLocal = selectedInfo.Type == "Local";
                _actions.SetEnabled(Actions.RemoveFileSystem, !isLocal);
                _removeButton.Enabled = !isLocal;

            };
        }

        private void NewS3FileSystem(object sender, EventArgs args)
        {
            FileSystemInfo info = new FileSystemInfo();
            info.Type = "S3";
            info.Caption = "(new)";

            _currentInfos.Add(info);
            this.UpdateData(_currentInfos.Count - 1);
        }

        private void SetupActions()
        {
            _actions.NewAction(Actions.AddFileSystem, "Add", UIManager.Instance.GetSmallImageListIndex("Add"), null, this.NewS3FileSystem);

            _actions.NewAction(Actions.RemoveFileSystem, "Remove", UIManager.Instance.GetSmallImageListIndex("Remove"), null, delegate
            {
                if (_driveList.SelectedIndices.Count != 1)
                {
                    return;
                }
                if (_driveList.SelectedIndices[0] == 0)
                {
                    return;
                }

                int i = _driveList.SelectedIndices[0];

                _currentInfos.RemoveAt(i);

                this.UpdateData(i-1);
             
            });

            ActionToButton(_removeButton, Actions.RemoveFileSystem);
            ActionToButton(_addS3Button, Actions.AddFileSystem);

        }

        private void ActionToButton(Button button,string action)
        {
            button.TextImageRelation = TextImageRelation.ImageBeforeText;
            button.Image = _actions.GetIcon(action).ToBitmap();
            button.Click += delegate
            {
                _actions.Run(action);
            };
        }

        private void UpdateData(int selectedIndex)
        {
            _driveList.Items.Clear();

            _driveList.SmallImageList = UIManager.Instance.SmallImageList;

            foreach (FileSystemInfo info in _currentInfos)
            {

                Dictionary<string, string> map = new Dictionary<string, string>();
                map["S3"] = "S3";
                map["Local"] = "Drive";

                ListViewItem item = _driveList.Items.Add(
                    info.Caption,
                    UIManager.Instance.GetSmallImageListIndex(map[info.Type]));
                item.Tag = info;

            }

            _driveList.FullRowSelect = true;
            _driveList.Items[selectedIndex].Selected = true;

            _descriptionLabel.Text = (_currentInfos.Count == 1) ?
                Properties.Messages.Options_Description_Add :
                (_manager.IsValid?Properties.Messages.Options_Description:Properties.Messages.Options_Description_Reverify);

        }

   

     
    }
}

